<?php
class My_API_getProfile
{
    private $_config;

    public function __construct()
    {
        $config = new Zend_Config_Ini(CONFIG_PATH2 . "/getprofile.ini",APPLICATION_ENV);
        $this->_config = $config;
    }
     public function getdataUserID($data)
    {
        $arrGame=array('ss31', 'ss32');
        if(isset($data['gameId']) && in_array($data['gameId'], $arrGame)){
            if($data['gameId']=='ss32'){
                return array("{$data['userList']}"=>array('userId'=>'b100001649019985'));
            }
            $model=new Default_Model_Channelconvertid($data['gameId']);
            $result=$model->checkUserName($data['userList']);
            if($result){
                $result=$result->toArray();
                return array($data['userList']=>array('userId'=>$result['id'] ));
            }
        }
        else{//cac game con lai
            if($data['channel'] != 'SSGROUP'){
            $model= new Default_Model_Convertid($data['gameId'], $data['channel']);
            $result=$model->checkUserName($data['userList']);
            if($result){
                $result=$result->toArray();
                return array($data['userList']=>array('userId'=>$result['id'],'mobile'=>'01237130860', 'email'=>'thanhloc@gmail.com' ));
            }
        }
        if(!isset($this->_config->getprofile)){
            return false;
        }
        $wsdl       = $this->_config->getprofile->wsdl;
        $agentID    = $this->_config->getprofile->agentId;
        $agentKey   = $this->_config->getprofile->agentKey;
        $secret     = $this->_config->getprofile->secret;
        $function   = $this->_config->getprofile->function;
        $arguments['argument0']   = $agentID;
        $arguments['argument1']   = $agentKey;
        $arguments['argument2']   = $data['userList'];
        $arguments['argument3']   = $data['channel'];  
        $arguments['argument4']   = md5($arguments['argument1'] . md5($arguments['argument2'] . $arguments['argument3']).$secret);
        try{
    		$client = new Zend_Soap_Client($wsdl);
    		$result = $client->$function($arguments);
            if($result['status'] != 1){
                return false;
            }
        }catch(Exception $ex){
            #echo $ex->getMessage();
            return false;
        }
        return $result['result'];
        }//end cac game con lai
        
    }
    public function getUserID($data){
        if(!isset($this->_config->getprofile)){
            return false;
        }
        $wsdl       = $this->_config->getprofile->wsdl;
        $agentID    = $this->_config->getprofile->agentId;
        $agentKey   = $this->_config->getprofile->agentKey;
        $secret     = $this->_config->getprofile->secret;
        $function   = $this->_config->getprofile->function;
        
        $arguments['argument0']   = $agentID;
        $arguments['argument1']   = $agentKey;
        $arguments['argument2']   = $data['userList'];
        $arguments['argument3']   = (isset($data['channel']) && $data['channel']) ? $data['channel'] : 'SSGROUP';
        $arguments['argument4']   = md5($arguments['argument1'] . md5($arguments['argument2'] . $arguments['argument3']) . $secret);
        try{
    		$client = new Zend_Soap_Client($wsdl);
    		$result = $client->$function($arguments);
            if($result['status'] != 1){
                return false;
            }
        }catch(Exception $ex){
            #echo $ex->getMessage();
            return false;
        }
        return $result['result'];
    }
    public function getDataUserName($data){
        if( $data['channel']!='SSGROUP'){
            $model=new Default_Model_Convertid($data['gameId'], $data['channel']);
            $arrUser[]=array();
            $arrUser=explode(",",$data['userIdList']);
            $mang=  array_unique ($arrUser);//lay ra duoc mang cac userid
            foreach($mang as $key => $val){ 
                if(!is_numeric($val) ){//neu phan tu nao ko phai la ki tu so thi cho vao
                    preg_match("/(\d+)(.)(.*)/i", $val, $outputArray);
                    $id=$outputArray[1];//lay ra userid la so
                    $chan=strtoupper($outputArray[3]);  //lay ra phan tu phia sau ki hieu dat biet
                    $dataUser=$model->getData($id); 
                    if($chan!= 'SSGROUP' && $dataUser){
                          $dataUser=$dataUser->toArray();
                          $key=$id.$outputArray[2].$outputArray[3];// kq se co dang 4345@zing hoac 4352_nct
                          preg_match("/(.*)([\!\@\#\$\%\^\&\*\_\-\+])(.*)/i", $dataUser['username'], $outputUsername);
                          $name=$outputUsername[1];
                          $result=array($key=>$name);
                          return $result;/// tra ve kq dang   array(4@zing=>vurapro)
                    }
                }
                else{
                    continue;
                }  
            }
        }else{
            $arrUser[]=array();
            $arrUser=explode(",",$data['userIdList']);
            foreach($arrUser as $key=> $val)
            {
                if(!is_numeric($val) ){
                    unset($arrUser[$key]);
                    }
            }
            $arrUser = implode(',',$arrUser); 
            return $this->getUserName(array('userIdList'=>$arrUser,'channel'=>$data['channel']));
            }
    }
    public function getUserName($data){
        if(isset($data['gameId']) && $data['gameId']=='ss31'){
            $modelConverid=new Default_Model_Channelconvertid($data['gameId']);
            $dataUser=$modelConverid->getData($data['userList']);
            if($dataUser){
                return $dataUser->toArray();
            }
            else{
                return;
            }
        }
        else{
        if(!isset($this->_config->getprofile)){
            return false;
        }
        $wsdl       = $this->_config->getprofileID->wsdl;
        $agentID    = $this->_config->getprofileID->agentId;
        $agentKey   = $this->_config->getprofileID->agentKey;
        $secret     = $this->_config->getprofileID->secret;
        $function   = $this->_config->getprofileID->function;
        $arguments['argument0']   = $agentID;
        $arguments['argument1']   = $agentKey;
        $arguments['argument2']   = $data['userIdList'];
        $arguments['argument3']   = (isset($data['channel']) && $data['channel']) ? $data['channel'] : 'SSGROUP';
        $arguments['argument4']   = md5($arguments['argument1'] . md5($arguments['argument2'] . $arguments['argument3']) . $secret);
        try{
    		$client = new Zend_Soap_Client($wsdl);
    		$result = $client->$function($arguments);
            if($result['status'] != 1){
                return false;
            }
        }catch(Exception $ex){
            //echo $ex->getMessage();exit;
            return false;
        }
        return $result['result'];
        }
    }
}